package com.bytech.common.config;

import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

import com.google.common.collect.ImmutableList;

/**
 * 博客上传功能配置类
 *
 * @Author Zhao
 * @Create 2021/11/22 21:31
 */
@Configuration
public class UploadConfig {

    public static final List<String> ACCEPT_IMAGE_EXTENSIONS = ImmutableList.of("gif", "jpg", "png", "jpeg");
    public static final List<String> ACCEPT_MUSIC_EXTENSIONS = ImmutableList.of("mp3");

    @Value("${upload.domain}")
    private String uploadDomain;
    @Value("${upload.path}")
    private String uploadPath;
    @Value("${upload.image.path}")
    private String imagePath;
    @Value("${upload.music.path}")
    private String musicPath;
    @Value("${upload.file.size}")
    private Long uploadMaxSize;
    @Value("${upload.file.totalSize}")
    private Long uploadMaxTotalSize;

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大
        factory.setMaxFileSize(DataSize.ofKilobytes(uploadMaxSize)); //KB

        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.ofMegabytes(uploadMaxTotalSize));
        return factory.createMultipartConfig();
    }

    public String getUploadLocation(String fullFilePath) {
        return uploadDomain + fullFilePath;
    }

    public String getBaseUploadFolder() {
        return uploadPath;
    }

    public String getImageFolder() {
        return uploadPath + imagePath;
    }

    public String getFullImageFolder(String folder) {
        return uploadPath + imagePath + folder;
    }

    public String getMusicFolder() {
        return uploadPath + musicPath;
    }

    public String getFullMusicFolder(String folder) {
        return uploadPath + musicPath + folder;
    }
}
